home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 3437 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.5 KB  |  127 lines

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Simple C program to list contents of a file - help!
  5. Date: Sun, 28 Jan 96 23:15:50 GMT
  6. Organization: none
  7. Message-ID: <822870950snz@genesis.demon.co.uk>
  8. References: <4egks8$nlu@usenet.ucs.indiana.edu>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <4egks8$nlu@usenet.ucs.indiana.edu>
  15.            gompa@nickel.ucs.indiana.edu "Raghu R. Gompa" writes:
  16.  
  17. >
  18. >I am trying to figure out what is wrong with this
  19. >program.  The following is written for dos environment:
  20. >"typeme.c"
  21. >
  22. >It is supposed to get words from file and list them with
  23. >arguments like help me please with the command:
  24. >     typeme -f file help me please
  25. >
  26. >Depending on the number of arguments, I sometimes getting
  27. >strange error messages - system stuck some times.
  28. >Please help.  This is part of a project (not a homework)
  29. >I am trying to complete. .. Thanks in advance. .. Raghu
  30. >
  31. >
  32.  
  33. >#include "stdio.h"
  34. >#include "string.h"
  35.  
  36. That should be
  37.  
  38. #include <stdio.h>
  39. #include <string.h>
  40.  
  41. >FILE *fopen(), *fp;
  42. >extern exit();
  43.  
  44. Don't try to declare standard functions yourself - that is what the
  45. header files are for. For exit() you need:
  46.  
  47. #include <stdlib.h>
  48.  
  49. >main(int argc, char *argv[])
  50. >{
  51. >char first[100], lett, *words[100], filename[15];
  52. >int numwords, cont=1;
  53. >int i, j, repeat=10, r,k=0,t;
  54. >int nrem;
  55. >if(*argv[1] == '-') {
  56. >    strcpy(filename, argv[2]);
  57. >    fp= fopen(filename,"r");
  58. >    if(fp == NULL) printf("Can't open %s \n",filename);
  59.  
  60. It is good that you are testing for failure. Unfortunately when it does fail
  61. you are falling through to the subsequent code which will then try to pass
  62. a null pointer to fscanf and so on which is illegal. If you have nothing
  63. better to do here exit the program with, say, exit(EXIT_FAILURE);
  64.  
  65. >    k=0;*words[1]='1';
  66.  
  67. You have defined words as an array of 100 (uninitialised) pointers. Here
  68. you are truing to dereference one of those unilitialised pointers which
  69. is illegal. You need to set each pointer to point to something before
  70. dereferencing it e.g. by using malloc.
  71.  
  72. >    while(fscanf(fp, "%s", words[k]) != EOF ) {
  73.  
  74. The same thing applies here.
  75.  
  76. >    k++;
  77.  
  78. You should test for this overflowing your array (>= 100 in this case).
  79. However it is better not to use 'magic numbers' through the program but
  80. rather (for example) #define a constant and us that throughout. That way
  81. if you need to change the value you can do it at one point and you're
  82. much less likely to have problems with the code due to oversights.
  83.  
  84. >    }
  85. >    fclose(fp);
  86. >    numwords=k-1;
  87. >    if(argc>2) {
  88. >          for(i=3;i<=argc;i++) {
  89.  
  90. You're going one too far here.
  91.  
  92. argv[0] is the program name
  93. argv[1] to argv[argc-1] are the arguments
  94. argv[argc] is a null pointer.
  95.  
  96. So in effect argc counts the program name as 1 argument.
  97.  
  98. >             strcpy(words[k],argv[i]);
  99. >             k++;
  100. >             }
  101. >          }
  102. >    numwords=k-1;
  103. >   }
  104. >else
  105. >   {
  106. >   numwords=argc-1;
  107. >    for(k=0;k<numwords;k++)
  108. >          strcpy(words[k],argv[k+1]);
  109.  
  110. Same pointer and range problems here.
  111.  
  112. >   }
  113. >
  114. >   nrem=1;
  115. >   for(i=0;i< numwords;i++) {
  116. >     printf("%3d. %10s", i+1, words[i]);
  117. >     nrem=(i%4);
  118. >     if(i>3 && !nrem) printf("\n");
  119. >     }
  120. >}
  121.  
  122. -- 
  123. -----------------------------------------
  124. Lawrence Kirby | fred@genesis.demon.co.uk
  125. Wilts, England | 70734.126@compuserve.com
  126. -----------------------------------------
  127.